import { getLocalStorageWithAutoParse , messages } from "share/common";
import { COMMON_REPORT_CONFIG, CONTRACT_DETAIL_COLUMNS } from "../constant";
import ContractReportService from '../report-private-service/contract-report.service.js'
import { commonUtilsFactory, toggleShowLabel, popoverTitle } from "../echarts-data-transfer/utils";
import Charts from "components/template/graphic-report/report-echarts";
import React from "react";
import VScrollEchartsV2 from "components/template/graphic-report/vscroll-echarts";
import ReportTable from "components/template/graphic-report/report-table";
import GraphicReportService from "../graphic-report.service";


const reportKeyPrefix = 'hly.reportLabel.41.';
const contractSignDateLabelKey = `${reportKeyPrefix}contractSignDateReport`;
const contractStatusLabelKey = `${reportKeyPrefix}contractStatusReport`;
const contractCompanyLabelKey = `${reportKeyPrefix}contractCompanyReport`;
const contractTypeLabelKey = `${reportKeyPrefix}contractTypeReport`;

function exportUtilsFactory({ reportType, exportType, context, config, allowSetting }) {
  const reportCode = 'CONAR';
  let ref = config.ref || {
    state: {}
  };
  let { list = [] } = ref.state || {}; // 表格是否有数据看reportTable的list
  let { dataLength = 0 } = config // 图形报表是否有数据看dataLength
  let exportDisable = !(list.length || dataLength);
  const { reportId } = context.state;
  let transferredParams = GraphicReportService.getCurrentTransferredParams({ context });
  let exportParams = { reportSearchDTO: { id: reportId, ...transferredParams } };
  return commonUtilsFactory({
    context,
    config,
    reportType,
    exportType,
    reportKeyPrefix,
    allowSetting,
    allowExport: true,
    reportCode,
    exportDisable,
    exportParams
  });
}

export default (context) => {
  let contractSignDateShowLabel = getLocalStorageWithAutoParse(contractSignDateLabelKey);
  if (contractSignDateShowLabel === undefined) {
    contractSignDateShowLabel = true;
  }

  let contractStatusShowLabel = getLocalStorageWithAutoParse(contractStatusLabelKey);
  if (contractStatusShowLabel === undefined) {
    contractStatusShowLabel = true;
  }

  let contractCompanyShowLabel = getLocalStorageWithAutoParse(contractCompanyLabelKey);
  if (contractCompanyShowLabel === undefined) {
    contractCompanyShowLabel = true;
  }

  let contractTypeShowLabel = getLocalStorageWithAutoParse(contractTypeLabelKey);
  if (contractTypeShowLabel === undefined) {
    contractTypeShowLabel = true;
  }

  return {
    contractSignDateReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key188')/*合同签署日期分析*/,
      fetchMethod: ContractReportService.getContractSignDateReport,
      showLabel: contractSignDateShowLabel,
      activeConfig: {
        key: 'filterMonth',
        array: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData, reportConfig } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'CONTRACT_DUE';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });

        return (
          <Charts
            allowActivated
            title={title}
            cols={15}
            utils={utils}
            dataLength={dataLength}
            options={options}
            reportKey='contractSignDateReport'
            reportType='tendency'
            isFetchingData={isFetchingData}
            loading={isLoading}
            compareKey='monthName'
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    contractStatusReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key189')/*合同状态分析*/,
      fetchMethod: ContractReportService.getContractStatusReport,
      showLabel: contractStatusShowLabel,
      activeConfig: {
        key: 'filterContractStatus',
        array: true
      },
      render: ({ config, context, reportType }) => {

        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'CONTRACT_STATUS';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });

        return (
          <Charts
            allowActivated
            title={title}
            cols={9}
            utils={utils}
            dataLength={dataLength}
            options={options}
            reportKey='contractStatusReport'
            reportType='pie'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    contractCompanyReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key190')/*合同公司分析*/,
      fetchMethod: ContractReportService.getContractCompanyReport,
      showLabel: contractCompanyShowLabel,
      activeConfig: {
        key: 'filterCompanyOids',
        array: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'CONTRACT_COMPANY';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='contractCompanyReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    contractTypeReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key191')/*合同类型分析*/,
      fetchMethod: ContractReportService.getContractTypeReport,
      showLabel: contractTypeShowLabel,
      activeConfig: {
        key: 'filterContractTypes',
        array: true
      },
      render: ({ config, context, reportType }) => {
        let { isFetchingData } = context.state;
        let { isLoading, options, dataLength, title } = config;

        const exportType = 'CONTRACT_TYPE';
        const utils = exportUtilsFactory({ reportType, exportType, context, config, allowSetting: true });
        options = toggleShowLabel({ config, options });
        return (
          <VScrollEchartsV2
            allowActivated
            title={title}
            dataLength={dataLength}
            options={options}
            cols={12}
            utils={utils}
            reportKey='contractTypeReport'
            isFetchingData={isFetchingData}
            loading={isLoading}
            onActivatedItemChange={context.onChartClick}
            onRef={context.onMountCharts}
          />
        )
      }
    },
    contractDetailReport: {
      ...COMMON_REPORT_CONFIG,
      title: messages('finance-7.key192')/*合同数据明细统计表*/,
      fetchMethod: ContractReportService.getContractDetailReport,
      isTable: true,
      columns: popoverTitle(CONTRACT_DETAIL_COLUMNS),
      render: ({ config, context, reportType }) => {
        const exportType = 'CONTRACT_DETAIL';
        const utils = exportUtilsFactory({ exportType, context, config, allowSetting: false });
        return (
          <ReportTable
            title={config.title}
            cols={24}
            utils={utils}
            columns={config.columns}
            fetchMethod={config.fetchMethod}
            onRef={context.onMountCharts}
            dataLength={1}
            reportKey='contractDetailReport'
          />
        )
      }
    }
  }
}
